<?php
require_once 'App/Config.php';
require_once 'App/View.php';
require_once 'App/Bootstraps.php';
class Zone_Action extends Zone_Config{

	public $_STOP	 = false;
	
	protected $roles = array();
	
	function __construct(){
		$this->view = new Zone_view();
		$this->view->_Controller = $this;
		$this->_params = array();
	}
	
	//should empty
	public function initAction(){	}
	
	public function run( $env = TRUE ){
		$this->env = $env;
		$this->view->setLanguage( $this->language );
		$this->view->setModule( $this->module );
		$this->view->setController( $this->controller );
		$this->view->setAction( $this->action );
		
		$this->setLayout( $this->getModuleName() );
		
		//offer model
		include_once 'App/Model.php';
		$adapter 	= $this->getConfig('adapter');
		$dbconfig 	= $this->getConfig('db');
		
		$db = Zend_Db::factory($adapter, $dbconfig);
		$this->Model = new App_Model( array('db' => $db));


		//append some unitily functions if exists
		$helper_file = ROOT.'/apps/modules/Helpers.php';
		
		if( file_exists($helper_file) ){
			require_once $helper_file;
		}
		
		//excute bootstraps for all module
		$bootstrap_file = ROOT.'/apps/modules/Bootstraps.php';
		
		$ctr = $this;
		if( file_exists($bootstrap_file) ){
			require_once $bootstrap_file;
			if( class_exists('Bootstraps')){
				$boot = new Bootstraps();
				$boot->app = $this;	
				$boot->env = $env;
				$boot->setModule( $this->module );	
				$boot->setController( $this->controller );	
				$boot->setAction( $this->action );	
				$boot->setLanguage( $this->language );	
				$boot->setParams( $this->params );
				$boot->Model = $this->Model;
				
				if( method_exists( $boot,'init')){
					$boot->init( $this );
				}
				
				$initModule = 'init'.$this->getModuleName();
				if( method_exists( $boot,$initModule )){
					$boot->$initModule( $this );
				}
				$ctr = $boot->app;
			}
		}


		//append some unitily functions of module if exists
		$helper_file = ROOT.'/apps/modules/'.$this->getModuleName().'/Helpers.php';
		
		if( file_exists($helper_file) ){
			require_once $helper_file;
		}		
		
		//excute bootstraps for current module
		$bootstrap_module_file = ROOT.'/apps/modules/'.$this->getModuleName().'/Bootstraps.php';
	
		if( !$ctr->_STOP ){	
			if( file_exists($bootstrap_module_file) ){
				require_once $bootstrap_module_file;
				$boot_module_name = $this->getModuleName().'Bootstraps';
				if( class_exists($boot_module_name)){
					$boot = new $boot_module_name();
					$boot->app = $this;	
					$boot->env = $env;
					$boot->setModule( $this->module );	
					$boot->setController( $this->controller );	
					$boot->setAction( $this->action );	
					$boot->setLanguage( $this->language );	
					$boot->setParams( $this->params );
					$boot->Model = $this->Model;
					
					if( method_exists( $boot,'init')){
						$boot->init( $this );
					}
					
					$initController = 'init'.$this->getControllerName();
					if( method_exists( $boot,$initController )){
						$boot->$initController( $this );
					}
					$ctr = $boot->app;
				}
			}		
		}	
		
		//next call action method	
		$action = $ctr->getActionName();
		
		//call a function before all real action
		if( !$ctr->__STOP ){
			$ctr->initAction();
		}
		
		//operacting current action
		if( !$ctr->__STOP ){
			$ctr->$action();
		}
		
		//whether overwrite html
		$ctr->view->overWriteHTML = $ctr->overWriteHTML;
		
		if( $env )
			$ctr->view->display();
		else
			return $ctr->view->getContent();
	}
	
	public function stop(){
		$this->__STOP = true;
	}
	
	public function stopEnv(){
		if( $this->env === true ){
			$this->setError();
			exit;
		}
	}
	
	public function set( $arr, $value = NULL){
		if( !$this->view->__Data ){
			$this->view->__Data = array();
		}
		if( is_array($arr)){
			foreach( $arr as $k => $v ){
				$this->view->__Data[$k] = $v;
			}
		}else{
			$this->view->__Data[$arr] = $value;
		}
	}
	
	public function setConfig( $config ){
		$this->config = $config;	
	}

	public function setLanguage( $language ){
		$this->language = $language;	
	}
	
	public function setModule($module){
		$this->module = $module;	
	}

	public function setController($controller){
		$this->controller = $controller;	
	}

	public function setAction($action){
		$this->action = $action;	
	}

	public function setParams( $params ){
		$this->_params = $params;
	}
	
	public function getParam( $k, $df='' ){
		return isset($this->_params[$k]) ? $this->_params[$k] : $df;	
	}
	
	public function setContent( $html ){
		$this->overWriteHTML = $html;
	}

	public function setJSON( $data ){
		die(json_encode( (array)$data ));
	}

	public function setJSONScript( $script, $data ){
		die("<script type='text/javascript'> var $script =".json_encode( (array)$data )."; </script>");
	}
	
	public function setLayoutJSON( $bool = true ){
		$this->view->setLayoutJSON( $bool );
	}
		
	public function setLayout( $layout ){
		$this->view->setLayout( $layout );
	}
	
	public function setView( $view ){
		$this->view->viewName = $view;
	}

	public function removeLayout(){
		$this->view->layoutPath = NULL;	
	}
	
	/*
		Ui method
	*/

	public function getModule(){
		return $this->module;
	}
	
	public function getController(){
		return $this->controller;
	}
	
	public function getAction(){
		return $this->action;
	}	
	
	public function getActionName( $class_name = TRUE ){
		return $class_name 
			? strtolower($this->action).'Action'
			: ucfirst(strtolower($this->action));
	}
	
	public function getControllerName(){
		return $this->getModuleName().ucfirst(strtolower($this->controller)).'Controller';
	}
	
	public function getModuleName(){
		return ucfirst(strtolower($this->module));
	}
	
	
	
	/* Place holder */
	public function placeHolder( $index, $html ){
		if( !$this->view->__placeHolder ){
			$this->view->__placeHolder = array();
		}
		if( isset($this->view->__placeHolder[$index]) ){
			$this->view->__placeHolder[$index] = array();
		}
		$this->view->__placeHolder[$index][] = $html;	
	}

	public function setError(){
		//@header('HTTP/1.1 404 Not Found');
		include ROOT.'/apps/layouts/404.phtml';
		exit;
	}
	
	public function removeCacheAction( $module, $controller='index', $action='index', $language ='', $args = array()){	
		$cache_path = 'apps/caches/actions/';
		$cache_file_name = urlencode($module.'_'.$controller.'_'.$action.'_'.implode('-',$args)).'.zone';
		return @unlink($cache_path.$cache_file_name);
	}
	
	/*a quick way to redirect in ajax */
	public function directScript( $url ='' ){
		$this->removeLayout();
		$this->setContent("<script>
			location.href = \"".addslashes($url)."\";
		</script>");	
	}
	
	/* The best way for ACL */
	public function setRole( $roles=array() ){
		$this->roles = (array)$roles;	
	}
	
	public function hasRole( $role ){
		return in_array( $role, $this->roles );
	}	
	
	/* Cache method extend from view */
	public function startCache( $name ){
		$this->view->startCache( $name );
	}

	public function stopCache( $name ){
		$this->view->stopCache( $name );
	}
	
	public function getContentCache( $name ){
		$this->view->getContentCache( $name );
	}

	public function getCache( $name ){
		$this->view->getCache( $name );
	}
	
	public function removeCache( $name ){
		$this->view->removeCache( $name );
	}	
	
	
	
	
	
}